package com.example.tm_androidbasics.tasks;

import java.util.Date;

import com.example.tm_androidbasics.MainActivity;

import android.app.Activity;
import android.os.AsyncTask;

public class UITask extends AsyncTask<Integer, Double, Date> {
	private MainActivity activity_;
	
	public UITask(MainActivity activity) {
		super();
		activity_ = activity;
	}
	
	
	@Override
	protected void onProgressUpdate(Double... values) {
		activity_.UpdateProgress(values[0]);
	}

	@Override
	protected Date doInBackground(Integer... countdown) {
		int intial = countdown[0];

		while (intial-- > 0) {
			publishProgress((double) intial / (double) countdown[0] * 100.0);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		return (new Date());
	}

	@Override
	protected void onPostExecute(Date result) {
		activity_.UpdateResult(result);
	}
}
